﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Framework;
using System.Data;
using CNVP.Framework.Helper;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin.CommonWindows
{
    public partial class Product_Select_Window : System.Web.UI.Page
    {
        BLL.CNVP_Products bll = new BLL.CNVP_Products();
        public int rplistCount = 0;
        int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                StartLoad(PageIndex);
                btUp.Attributes.Add("onclick", "if(!ifCheck()){alert('请选择销售商品！');return false;};");
            }
        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dt = null;
            string textKey = Request.QueryString["textKey"];
            int textNum = 8;
            SQLConditionInfo[] st = new SQLConditionInfo[1];
            st[0] = new SQLConditionInfo("@textKey", Public.FilterSql(textKey));
            dt = bll.GetPageKey(this.GetType().Name, PageIndex, textNum, out i, out j, st);
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    rplistCount = dt.Rows.Count;//行数
                    //----------------------------------------添加列------------------------------------------------
                    dt.Columns.Add("oID", typeof(string));
                    dt.Columns.Add("oProDetailNo", typeof(string));
                    dt.Columns.Add("oStockNum", typeof(string));
                    dt.Columns.Add("oSalePrice", typeof(string));
                    dt.Columns.Add("oProductName", typeof(string));
                    dt.Columns.Add("oProDetailID", typeof(string));
                    dt.Columns.Add("oStandardValueIDs", typeof(string));
                    dt.Columns.Add("oStandardValues", typeof(string));
                    dt.Columns.Add("oID_ProDetailID", typeof(string));
                    //----------------------------------------添加列结束--------------------------------------------
                    for (int k = 0; dt.Rows.Count > k; k++)
                    {
                        dt.Rows[k]["oID"] = dt.Rows[k]["ID"];
                        var _ProductName = dt.Rows[k]["ProductName"];
                        string _ProductNameHref = "<a href=\"/ProductDetail_" + dt.Rows[k]["ProductClassID"] + "_" + dt.Rows[k]["ID"] + ".aspx\" target=\"_blank\" title=\"" + _ProductName + "\">" + BasePage.CutString(dt.Rows[k]["ProductName"].ToString(), 20) + "</a>";
                        dt.Rows[k]["oProductName"] = _ProductNameHref;

                        string _ProDetailNo = Convert.ToString(dt.Rows[k]["ProDetailNo"]);
                        string _SalePrice = Convert.ToString(dt.Rows[k]["SalePrice"]);
                        string _StockNum = Convert.ToString(dt.Rows[k]["StockNum"]);
                        if (string.IsNullOrEmpty(_SalePrice))
                        {
                            _ProDetailNo = Convert.ToString(dt.Rows[k]["ProductNo"]);
                        }
                        if (string.IsNullOrEmpty(_SalePrice))
                        {
                            _SalePrice = Convert.ToString(dt.Rows[k]["TradePrice"]);
                        }
                        if (string.IsNullOrEmpty(_StockNum))
                        {
                            _StockNum = Convert.ToString(dt.Rows[k]["StockTotalNum"]);
                        }
                        dt.Rows[k]["oProDetailNo"] = _ProDetailNo;
                        dt.Rows[k]["oSalePrice"] = _SalePrice;
                        dt.Rows[k]["oStockNum"] = _StockNum;
                        string _ProDetailID = string.IsNullOrEmpty(Convert.ToString(dt.Rows[k]["ProDetailID"])) ? "0" : Convert.ToString(dt.Rows[k]["ProDetailID"]);
                        dt.Rows[k]["oProDetailID"] = _ProDetailID;
                        dt.Rows[k]["oID_ProDetailID"] = dt.Rows[k]["ID"] + "_" + _ProDetailID;
                        string _StandardValueIDs=Convert.ToString(dt.Rows[k]["StandardValueIDs"]);
                        if (!string.IsNullOrEmpty(_StandardValueIDs))
                        {
                            string[] StandardIDArray = _StandardValueIDs.Split(Convert.ToChar(","));
                            string StandardValueName = "(";
                            for (int a = 0; a < StandardIDArray.Length; a++)
                            {
                                var model_StandardValue = new BLL.CNVP_StandardValue().GetSingle_CNVP_StandardValueById(int.Parse(StandardIDArray[a]));
                                if (model_StandardValue != null)
                                {
                                    StandardValueName += model_StandardValue.StandardValueName + "、";
                                }
                            }
                            StandardValueName = StandardValueName.Substring(0, StandardValueName.Length - 1) + ")";
                            dt.Rows[k]["oStandardValueIDs"] = BasePage.CutString(StandardValueName, 25);
                            dt.Rows[k]["oStandardValues"] = StandardValueName;
                        }
                    }
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id_ProDetailID = e.CommandArgument.ToString();
            if (e.CommandName == "Select")
            {
                BasePage.CloseWindowReturnValues(id_ProDetailID);
            }
        }
        string strCode="";
        protected void btUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.rpList.Items.Count > 0)
                {
                    for (int i = 0; i < this.rpList.Items.Count; i++)
                    {
                        CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                        if (cbCheck.Checked == true)
                        {
                            strCode += cbCheck.ToolTip + ",";
                        }
                    }
                    strCode = strCode.Substring(0, strCode.Length - 1);
                    BasePage.CloseWindowReturnValues(strCode);
                }
            }
            catch
            { BasePage.CloseWindowReturnValues(strCode); }
        }
    }
}